package org.checkboo.math;


public class Vector2f {
	
	private float x;
	private float y;
	
	public Vector2f(float x, float y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	public Vector2f(Vector2f vector) {
		super();
		this.x = vector.x;
		this.y = vector.y;
	}
	
	public Vector2f(){
		super();
		this.x = 0;
		this.y = 0;
	}
	
	/**
	 * Adds supplied vector to this vector and stores it in this object.
	 * 
	 * @param vector vector to add
	 */
	public Vector2f add(Vector2f vector){
		this.x += vector.x;
		this.y += vector.y;
		return this;
	}
	
	/**
	 * Adds supplied vector to this vector and stores it in result.
	 * 
	 * @param second supplied vector to add
	 * 
	 * @param result object to store the result in
	 */
	public void addRes(Vector2f second,Vector2f result){
		result.x = this.x + second.x;
		result.y = this.y + second.y;
	}
	
	public Vector2f addNew(Vector2f second){
		return new Vector2f(this.x+second.x,this.y+second.y);
	}
	
	/**
	 * Multiplies vectors and stores it in this object.
	 * 
	 * @param second vector to multiply by
	 */
	public Vector2f multiply(Vector2f second){
		this.x *= second.x;
		this.y *= second.y;
		return this;
	}
	
	/**
	 * Multiplies vectors and stores it in result.
	 * 
	 * @param second vector to multiply by
	 * 
	 * @param result object to store the result in
	 */
	public void multiplyRes(Vector2f second, Vector2f result){
		result.x = this.x * second.x;
		result.y = this.y * second.y;
	}
	
	public Vector2f multiplyNew(Vector2f second){
		return new Vector2f(this.x*second.x,this.y*second.y);
	}
	
	/**
	 * Multiplies vector by scalar and stores it in this object.
	 * 
	 * @param scalar scalar to multiply by
	 */
	public Vector2f multiply(float scalar){
		this.x *= scalar;
		this.y *= scalar;
		return this;
	}
	
	/**
	 * Multiplies vector by scalar and stores it in result.
	 * 
	 * @param scalar scalar to multiply by
	 * 
	 * @param result object to store the result in
	 */
	public void multiplyRes(float scalar, Vector2f result){
		result.x = this.x * scalar;
		result.y = this.y * scalar;
	}
	
	public Vector2f multiplyNew(float scalar){
		return new Vector2f(this.x*scalar,this.y*scalar);
	}
	
	public Vector2f divide(Vector2f second){
		this.x /= second.x;
		this.y /= second.y;
		return this;
	}
	
	public Vector2f divide(float scalar){
		this.x /= scalar;
		this.y /= scalar;
		return this;
	}
	
	public void divideRes(Vector2f second, Vector2f result){
		result.x = this.x / second.x;
		result.y = this.y / second.y;
	}
	
	public void divideRes(float scalar, Vector2f result){
		result.x = this.x / scalar;
		result.y = this.y / scalar;
	}
	
	public Vector2f divideNew(Vector2f second){
		return new Vector2f(this.x/second.x,this.y/second.y);
	}
	
	public Vector2f divideNew(float scalar){
		return new Vector2f(this.x/scalar,this.y/scalar);
	}
	
	public Vector2f subtract(Vector2f second){
		this.x -= second.x;
		this.y -= second.y;
		return this;
	}
	
	public void subtractRes(Vector2f second, Vector2f result){
		result.x = this.x - second.x;
		result.y = this.y - second.y;
	}
	
	public Vector2f subtractNew(Vector2f second){
		return new Vector2f(this.x - second.x, this.y - second.y);
	}
	
	public float dot(Vector2f second){
		return this.x*second.x+this.y*second.y;
	}
	
	public static float dot(Vector2f first, Vector2f second){
		return first.x*second.x+first.y*second.y;
	}
	
	public float cross(Vector2f second){
		return this.x*second.y - this.y*second.x;
	}
	
	public static float cross(Vector2f first, Vector2f second){
		return first.x*second.y - first.y*second.x;
	}

	public float magnitude(){
		return FloatMath.sqrt(FloatMath.sqr(this.x)+FloatMath.sqr(this.y));
	}
	
	public Vector2f normalize(){
		float magnitude = this.magnitude();
		if(magnitude != 0){
			return divide(magnitude);
		}
		return this;
	}
	
	public Vector2f negate(){
		this.x = -this.x;
		this.y = -this.y;
		return this;
	}
	
	public Vector2f negateNew(){
		return new Vector2f(-this.x,-this.y);
	}	
	
	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}
	
	public String toString(){
		return "("+this.x+" , "+this.y+")";
	}
	
	public boolean equals(Vector2f second){
		if(null == second)
			return false;
		if(this.x == second.x && this.y == second.y)
			return true;
		else
			return false;
	}
	
}

